#!/usr/bin/perl

use strict;
use warnings;

my $BEST_VALUE = 88880000;
my $in_table   = 0;
my $row        = 0;
my %ants;
my %enemies;
my %cell;
my %agent_data;

my $agent;
LINE: for (<>) {
    chomp;
    if (/^\s*ant \((\d+),(\d+)\)/) {
        $ants{$1}{$2} = 1;
        next LINE;
    }

    if (/^\s*enemy \((\d+),(\d+)\)/) {
        $enemies{$1}{$2} = 1;
        next LINE;
    }

    if (/^\s*(\S+)\s*$/) {
        $agent = $1;
        $row   = 0;
        next LINE;
    }

    my $col = 0;
  CELL: for my $cell_value (split) {
        $cell{$row}{$col}{$agent}{'VALUE'} = $cell_value;

        $agent_data{$agent}{'VALUES'}{$cell_value} = 1;

        $agent_data{$agent}{'MAX'} = $cell_value
          if ( not $agent_data{$agent}{'MAX'}
            or $cell_value > $agent_data{$agent}{'MAX'} );

        $agent_data{$agent}{'MIN'} = $cell_value
          if ( not $agent_data{$agent}{'MIN'}
            or $cell_value < $agent_data{$agent}{'MIN'} );

        $col++;
    }
    print '</tr>', "\n";
    $row++;
}

for my $agent ( sort keys %agent_data ) {
    my @values = sort { $a <=> $b } keys %{ $agent_data{$agent}{'VALUES'} };

    #die "too many $agent values: " . scalar @values
    #	if scalar @values > 255;

    my $bucket_size = 255 / scalar @values;
    $agent_data{$agent}{'BUCKET'}{0} = '00';
    my $current_bucket = 0;
    for my $value (@values) {
        $current_bucket += $bucket_size;
        $agent_data{$agent}{'BUCKET'}{$value} =
          sprintf( "%02x", int($current_bucket) );
    }

#printf '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
#	$agent, $agent_data{$agent}{'MAX'}, $agent_data{$agent}{'MIN'},
#	$bucket_size,
#	join('<br>', @values),
#	join('<br>', map { $agent_data{$agent}{'BUCKET'}{$_} } @values);
}

print '<html><head>' . '' . '</head><body>';

print '<table border=0 cellspacing=0 width=100%>';

for my $agent ( sort keys %agent_data ) {

    print "<tr><th colspan='10'>$agent</th><td>\n";

    for my $row ( sort { $a <=> $b } keys %cell ) {
        print '<tr>';

        for my $col ( sort { $a <=> $b } keys %{ $cell{$row} } ) {

            my $value_red = $cell{$row}{$col}{$agent}{'VALUE'};
            my $hex_red   = $agent_data{$agent}{'BUCKET'}{$value_red} || '00';
            my $hex_blue  = '00';
            my $hex_green = '00';

            my $hex = sprintf '#%s%s%s', $hex_red, $hex_green, $hex_blue;

            my $text_color =
                 $ants{$row}{$col}
              || $enemies{$row}{$col}
              || $BEST_VALUE == $value_red ? '#fff' : '#333';
            my $hover_content = "($row,$col): $value_red";
            my $cell_content =
                $ants{$row}{$col}         ? 'A'
              : $enemies{$row}{$col}      ? 'E'
              : $BEST_VALUE == $value_red ? 'X'
              :                             '&nbsp';
            print
				"<td bgcolor=\"#${hex}\" title=\"$hover_content\" align=\"center\">"
              . "<span style=\"font-size:.8em;color:$text_color\">$cell_content</span>"
              . "</td>";
        }

        print '</tr>';
    }
}

print '</table>';
print '</body></html>';

